﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class frmcuttingguihuan : Form
    {
        string s1 = "";
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        string flag;
        string tb0, tb1, tb2, tb3, tb4, tb5, tb6;
        public frmcuttingguihuan(string flag,string tb0, string tb1, string tb2, string tb3, string tb4, string tb5, string tb6)
        {
            InitializeComponent();
            this.flag = flag;
            this.tb0 = tb0;
            this.tb1 = tb1;
            this.tb2 = tb2;
            this.tb3 = tb3;
            this.tb4 = tb4;
            this.tb5 = tb5;
            this.tb6 = tb6;
        }

        private void btn_歸還_Click(object sender, EventArgs e)
        {
            try
            {

                if (txt歸還數量_gh.Text == "")
                {
                    MessageBox.Show("請您輸入歸還數量", "錯誤", MessageBoxButtons.OK);
                    txt歸還數量_gh.Focus();
                    return;
                }
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    int check1 = Int32.Parse(txt領用數量_gh.Text.ToString());
                    int check2 = Int32.Parse(txt歸還數量_gh.Text.ToString());

                    if (check1 < check2)
                    {
                        MessageBox.Show("歸還數量很多（<=）" + check1);
                    }
                    //so luong tra bang so luong muon thi xoa trong csdl
                    else if (check2 <= 0)
                    {
                        MessageBox.Show("不能歸還！ 請再檢查！", "錯誤", MessageBoxButtons.OK);
                    }
                    else
                        if (check1 == check2 || check1 < 0)
                        {
                            //string sp_delete = "sp_刀具領用_刪除";
                            //SqlCommand sqldelete = new SqlCommand();
                            //sqldelete.Connection = con;
                            //sqldelete.CommandType = CommandType.StoredProcedure;
                            //sqldelete.CommandText = sp_delete;
                            //sqldelete.Parameters.AddWithValue("@項次", tb1);
                            ////MessageBox.Show("刪除成功");
                            //sqldelete.ExecuteNonQuery();
                            //string delete = tb1;
                            ////string delete1 = tbl6;
                            string delete = tb1;
                            string sp_increment = "sp_刀具領用_更新_-n";//tru ling yong
                            SqlCommand sqlincrement = new SqlCommand();
                            sqlincrement.Connection = con;
                            sqlincrement.CommandType = CommandType.StoredProcedure;
                            sqlincrement.CommandText = sp_increment;
                            sqlincrement.Parameters.AddWithValue("@項次", delete);
                            sqlincrement.Parameters.AddWithValue("@刀具編號", txt刀具編號_gh.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@領用數量", int.Parse(txt領用數量_gh.Text));
                            sqlincrement.Parameters.AddWithValue("@領用人代號", txt領用人代號_gh.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@領用部門", txt領用部門_gh.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@歸還日期", dateTime_歸還日期.Value);
                            sqlincrement.Parameters.AddWithValue("@歸還數量", int.Parse(txt歸還數量_gh.Text));
                            sqlincrement.Parameters.AddWithValue("@備註", txtghichu.Text.ToString());
                            sqlincrement.ExecuteNonQuery();
                            //Tra ve
                            string sp_update = "sp_刀具_更新_guihuan_+n"; // cong vao kho
                            SqlCommand cmd1 = new SqlCommand(sp_update, con);
                            cmd1.CommandType = CommandType.StoredProcedure;
                            cmd1.Parameters.AddWithValue("@刀具編號", txt刀具編號_gh.Text);
                            //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                            cmd1.Parameters.AddWithValue("@n", txt領用數量_gh.Text);
                            cmd1.ExecuteNonQuery();
                            MessageBox.Show("歸還成功");
                            con.Close();
                            this.Close();
                        }
                        //khi check1>check 2 thuc hien tra ve lingyong va tra ve kho
                        else
                        {
                            string delete = tb1;
                            //string delete1 = tbl6;
                            string sp_increment = "sp_刀具領用_更新_-n";//tru ling yong
                            SqlCommand sqlincrement = new SqlCommand();
                            sqlincrement.Connection = con;
                            sqlincrement.CommandType = CommandType.StoredProcedure;
                            sqlincrement.CommandText = sp_increment;
                            sqlincrement.Parameters.AddWithValue("@項次", delete);
                            sqlincrement.Parameters.AddWithValue("@刀具編號", txt刀具編號_gh.Text.ToString());
                            //sqlincrement.Parameters.AddWithValue("@領用日期", txt領用日期.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@領用數量", int.Parse(txt領用數量_gh.Text));
                            sqlincrement.Parameters.AddWithValue("@領用人代號", txt領用人代號_gh.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@領用部門", txt領用部門_gh.Text.ToString());
                            sqlincrement.Parameters.AddWithValue("@歸還日期", dateTime_歸還日期.Value);
                            sqlincrement.Parameters.AddWithValue("@歸還數量", int.Parse(txt歸還數量_gh.Text));
                            sqlincrement.Parameters.AddWithValue("@備註", txtghichu.Text.ToString());
                            sqlincrement.ExecuteNonQuery();

                            //Tra ve
                            string sp_update = "sp_刀具_更新_guihuan_+m"; // cong vao kho
                            SqlCommand cmd1 = new SqlCommand(sp_update, con);
                            cmd1.CommandType = CommandType.StoredProcedure;
                            cmd1.Parameters.AddWithValue("@刀具編號", txt刀具編號_gh.Text);
                            //cmd1.Parameters.AddWithValue("@量具狀態", txt量具狀態.Text.ToString());
                            cmd1.Parameters.AddWithValue("@m", txt歸還數量_gh.Text);
                            cmd1.ExecuteNonQuery();
                            MessageBox.Show("歸還成功");
                            con.Close();
                            this.Close();
                        }
                }
                
            }

            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void frmcuttingguihuan_Load(object sender, EventArgs e)
        {
            txt刀具編號_gh.Enabled = false;
            txt領用日期.Enabled = false;
            txt領用數量_gh.Enabled = false;
            txt領用人代號_gh.Enabled = false;
            txt領用部門_gh.Enabled=false;
            txt_項次.Enabled = false;
            //txtghichu.Enabled = false;
            txt刀具編號_gh.Text = tb0;//cb1 1
            txt_項次.Text = tb1;
            txt領用日期.Text = tb2;
            txt領用數量_gh.Text = tb3;
            txt領用人代號_gh.Text = tb4;
            txt領用部門_gh.Text = tb5;
            txtghichu.Text = tb6;
            //txt保管人代號.Text = tb7;
        }

        private void btn_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt領用數量_gh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txt歸還數量_gh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}
